package com.ease.common.util;

import org.apache.commons.lang.StringUtils;

public class IpAddressUtil {

	/**
	 * 173544729 -> 10.88.21.25
	 * 
	 * @return
	 */
	public static String fromDecimal(long decimalIpAddress) {

		String binaryString = asBinaryFromDecimal(decimalIpAddress);
		StringBuilder sb = new StringBuilder();
		sb.append(Integer.valueOf(binaryString.substring(0, 8), 2).toString()).append(".");
		sb.append(Integer.valueOf(binaryString.substring(8, 16), 2).toString()).append(".");
		sb.append(Integer.valueOf(binaryString.substring(16, 24), 2).toString()).append(".");
		sb.append(Integer.valueOf(binaryString.substring(24, 32), 2).toString());

		return sb.toString();
	}

	/**
	 * 173544729 -> 00001010010110000001010100011001
	 * 
	 * @return
	 */
	public static String asBinaryFromDecimal(long decimalIpAddress) {
		String binaryString = Long.toBinaryString(decimalIpAddress);
		binaryString = StringUtils.leftPad(binaryString, 32, '0');
		return binaryString;
	}

	/**
	 * 00001010010110000001010100011001 -> 173544729
	 * 
	 * @return
	 */
	public static String asDecimalFromBinary(String binaryIpAddress) {
		String binaryString = Long.valueOf(binaryIpAddress, 2).toString();
		return binaryString;
	}

	public static void main(String[] args) {
		// System.out.println(asBinaryFromDecimal(173544729));
		System.out.println(fromDecimal(3029697686L));		
//		System.out.println(asBinaryFromDecimal(4294967295L));
		
//		 System.out.println(asDecimalFromBinary("11111111111111111111111111111111"));
	}
}
